﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using Hr.BLLContainer;
using System.Data.Entity;
using Newtonsoft.Json.Linq;
using Hr.BLL;
using Hr.Model;
using HR.HrHelper;
using System.Web;

namespace HR.Areas.zhao_kuangtao.Controllers
{
    public class LoginController : ApiController
    {
        protected LoginService Login = new LoginService();
        int code = 0;
        string msg = "success";

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <returns>返回用户信息</returns>
        [HttpPost]
        public JObject GetUser()
        {
            System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;
            User_List u = JsonHelper.DeserializeJsonToObject<User_List>(RequestForm["UserInfo"]);//信息前后端对接无误
            msg = "登陆成功";
             u = Login.Login(u.UserName, u.Password);
            object ud = "";
            if (u == null)
            {
                code = 1;
                msg = "用户名或密码错误";
            }
            else if (u.IsLock ==true)
            {
                code = 1;
                msg = "用户已被禁用，无法登录";
            }
            else
            {
                u.LastLoginIP = ((System.Web.HttpContextWrapper)Request.Properties["MS_HttpContext"]).Request.UserHostAddress;
                u.LastLoginTime = DateTime.Now;
                u.EntryTime = DateTime.Now;
                u.Remark = "<i class=\" fa fa-circle text - success \"></i> 在线";
                Login.UpdateUser(u);
                ud = new { u.pk_User,u.Education,u.School,u.Phone1,u.Note,u.Address1, u.UserName, u.FullName, PositionName=Login.GetPosition(int.Parse(u.Position.ToString())), ImageData =FileHelper.GetImgInfo(u.ImagePath1), DepartmentName_CHS=Login.GetDepartment(int.Parse(u.fk_Department.ToString())) };
            }
            
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",ud}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 修改用户在线状态
        /// </summary>
        /// <param name="UserID"></param>
        [HttpPost]
        public JObject UpdateResume()
        {
            System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;
            int UserID = int.Parse(RequestForm["UserInfo"].ToString());//信息前后端对接无误
            string Remark = "<i class=\" fa fa-circle text - success \"></i> 离线";
            Login.UpdateUserRemark(UserID, Remark);
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
    }
}
